package category.server;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import category.server.db.DataSourceFactory;
import category.server.db.DbUtil;
import category.server.db.MDTMySQLRowMapper;
import category.server.db.QueryRunner;


public class AttributeDAO {
	public String hostName = "localhost";
	public String dbName = "zkm0m1_db";
	public String password = "ChinacaT";
	public String dbUser = "root";
	
	public List<Map> getAttributesByProductId(String id,String lang) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceFactory.getDataSource(hostName, dbName, dbUser, password), new MDTMySQLRowMapper());
		String sql = "SELECT * FROM attributes WHERE productID = "+ DbUtil.escSql(id) + " order by DisplayOrder";
		if("eng".equals(lang))sql = "SELECT * FROM attributes_eng WHERE productID = "+ DbUtil.escSql(id) + " order by DisplayOrder";
		if("big".equals(lang))sql = "SELECT * FROM attributes_big WHERE productID = "+ DbUtil.escSql(id) + " order by DisplayOrder";
		List<Map> m = runner.query(sql);
		if (m == null || m.size() == 0)
			return null;
		return m;
	}
	
	public String addModelNameToTitleByProductId(String id,String lang) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceFactory.getDataSource(hostName,dbName,dbUser,password), new MDTMySQLRowMapper());
		String sql = "SELECT * FROM attributes WHERE productID = "
				+ DbUtil.escSql(id) + " and AttrName="+DbUtil.escSql("型号")+" order by ProductID,DisplayOrder";
		if ("eng".equals(lang))
			sql = "SELECT * FROM attributes_eng WHERE productID = "
					+ DbUtil.escSql(id) + " and AttrName="+DbUtil.escSql("ModelNumber")+" order by ProductID,DisplayOrder";
		if ("big".equals(lang))
			sql = "SELECT * FROM attributes_big WHERE productID = "
					+ DbUtil.escSql(id) + "  and AttrName="+DbUtil.escSql("型號")+" order by ProductID,DisplayOrder";
		Map m = runner.queryForMap(sql);
		if (m == null || m.size() == 0)
			return null;
		return (String)m.get("AttrValue");
	}
}
